#f21010631徐唯嘉
import cv2
#二值化阈值处理
img=cv2.imread('fengjing.jpeg')
cv2.imshow('img',img)
ret,img2=cv2.threshold(img,150,255,cv2.THRESH_BINARY)	#二值化阈值处理
cv2.imshow('imgTHRESH_BINARY',img2)
cv2.waitKey(0)

#反二值化阈值处理
img=cv2.imread('fengjing.jpeg')
cv2.imshow('img',img)
ret,img2=cv2.threshold(img,150,255,cv2.THRESH_BINARY_INV)		#反二值化阈值处理
cv2.imshow('imgTHRESH_BINARY_INV',img2)
cv2.waitKey(0)

#截断阈值处理
img=cv2.imread('fengjing.jpeg')
cv2.imshow('img',img)
ret,img2=cv2.threshold(img,150,255,cv2.THRESH_TRUNC)	#截断阈值处理
cv2.imshow('imgTHRESH_TRUNC',img2)
cv2.waitKey(0)

#超阈值零处理
img=cv2.imread('fengjing.jpeg')
cv2.imshow('img',img)
ret,img2=cv2.threshold(img,150,255,cv2.THRESH_TOZERO)		#超阈值零处理
cv2.imshow('imgTHRESH_TOZERO',img2)
cv2.waitKey(0)

#低阈值零处理
img=cv2.imread('fengjing.jpeg')
cv2.imshow('img',img)
ret,img2=cv2.threshold(img,150,255,cv2.THRESH_TOZERO_INV)		#低阈值零处理
cv2.imshow('imgTHRESH_TOZERO_INV',img2)
cv2.waitKey(0)

#\Otsu算法阈值处理
img=cv2.imread('fengjing.jpeg',cv2.IMREAD_GRAYSCALE)			#读取图像，将其转换为单通道灰度图像
cv2.imshow('img',img)					#显示原图
ret,img2=cv2.threshold(img,127,255,cv2.THRESH_BINARY)		#阈值处理
cv2.imshow('img2',img2)
ret,img3=cv2.threshold(img,127,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)
cv2.imshow('img3',img3)
ret,img4=cv2.threshold(img,127,255,cv2.THRESH_BINARY_INV+cv2.THRESH_OTSU)
cv2.imshow('img4',img4)
cv2.waitKey(0)

#三角算法阈值处理
img=cv2.imread('fengjing.jpeg',cv2.IMREAD_GRAYSCALE)		#读取图像，将其转换为单通道灰度图像
cv2.imshow('img',img)					#显示原图
ret,img2=cv2.threshold(img,127,255,cv2.THRESH_BINARY)	#阈值处理
cv2.imshow('img2',img2)
ret,img3=cv2.threshold(img,127,255,cv2.THRESH_BINARY+cv2.THRESH_TRIANGLE)
cv2.imshow('img3',img3)
ret,img4=cv2.threshold(img,127,255,cv2.THRESH_BINARY_INV+cv2.THRESH_TRIANGLE)
cv2.imshow('img4',img4)
cv2.waitKey(0)

#自适应阈值处理
img=cv2.imread('fengjing.jpeg',cv2.IMREAD_GRAYSCALE) #读取图像，将其转换为单通道灰度图像
cv2.imshow('img',img)
img2=cv2.adaptiveThreshold(img,255,cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY,5,10) #阈值处理
cv2.imshow('img2',img2)
cv2.waitKey(0)

